/*
 * BinaryHeap.cpp
 *
 *  Created on: Sep 9, 2012
 *      Author: jeffrey
 */

#include "BinaryHeap.h"

namespace structure {

void BinaryHeap::insert(T val){
	this->base.push_back(val);
	this->up_heapfy(base.size()-1);
}
T BinaryHeap::pop() {
	if (!base.empty()) {
		T result=base[0];
		base[0] = base[base.size() - 1];
		base.pop_back();
		if (!base.empty())
			down_heapfy(0);
		return result;
	}

}
T BinaryHeap::top() const {
	return base[0];
}
void BinaryHeap::buildHeap(T array[], int len) {
	for (int i = 0; i < len; i++) {
		base.push_back(array[i]);
	}
	for (int i = parent(base.size() - 1); i >= 0; i--)
		down_heapfy(i);

}
} /* namespace structure */
